package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	_ "time"
)

type OrderTrade struct {
	Id            int     `json:"id" gorm:"type:int(11) unsigned;primary_key"` //
	OrderId       int     `json:"orderId" gorm:"type:int(11);"`                //
	TradeMemberId int     `json:"tradeMemberId" gorm:"type:int(11);"`          //
	TradeNo       string  `json:"tradeNo" gorm:"type:varchar(255);"`           //
	TradePrice    float64 `json:"tradePrice" gorm:"type:decimal(10,2);"`       //
	TradeBody     string  `json:"tradeBody" gorm:"type:text;"`                 //
	TradeAppId    string  `json:"tradeAppId" gorm:"type:varchar(255);"`        //
	TradeMechId   string  `json:"tradeMechId" gorm:"type:varchar(255);"`       //
	TradeOpenId   string  `json:"tradeOpenId" gorm:"type:varchar(255);"`       //
	TradeStart    string  `json:"tradeStart" gorm:"type:varchar(255);"`        //
	TradeCallUrl  string  `json:"tradeCallUrl" gorm:"type:varchar(255);"`      //
	TradeResult   string  `json:"tradeResult" gorm:"type:text;"`               //
	TradeInfo     string  `json:"tradeInfo" gorm:"type:text;"`                 //
	TradeStatus   string  `json:"tradeStatus" gorm:"type:varchar(255);"`       //
	DataScope     string  `json:"dataScope" gorm:"-"`
	Params        string  `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderTrade) TableName() string {
	return "order_trade"
}

// 创建OrderTrade
func (e *OrderTrade) Create() (OrderTrade, error) {
	var doc OrderTrade
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// GetByNO
// 获取OrderTrade
func (e *OrderTrade) GetByNO(tradeNo string) (OrderTrade, error) {
	var doc OrderTrade
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("trade_no = ?", tradeNo)
	if err := table.Order("id DESC", true).First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderTrade
func (e *OrderTrade) GetId() (OrderTrade, error) {
	var doc OrderTrade
	table := orm.Eloquent.Table(e.TableName())

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderTrade带分页
func (e *OrderTrade) GetPage(pageSize int, pageIndex int) ([]OrderTrade, int, error) {
	var doc []OrderTrade

	table := orm.Eloquent.Select("*").Table(e.TableName())

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderTrade
func (e *OrderTrade) Update(id int) (update OrderTrade, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderTrade
func (e *OrderTrade) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&OrderTrade{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderTrade) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&OrderTrade{}).Error; err != nil {
		return
	}
	Result = true
	return
}
